// The Vue build version to load with the `import` command
// (runtime-only or standalone) has been set in webpack.base.conf with an alias.
import Vue from 'vue'
import Vuex from 'vuex'
import VueRouter from 'vue-router'  //vue路由插件
import VueResource from 'vue-resource' //vueJSON插件
import MintUI from 'mint-ui'  //mint-ui插件js
import 'mint-ui/lib/style.css' //mint-ui插件c's's
import NutUI from '@nutui/nutui' //京东的UI插件js
import '@nutui/nutui/dist/nutui.css' //京东的UI插件css
// import mui from './mui-master/dist/js/mui.js' //MUI插件js
import './mui-master/dist/css/mui.min.css' //MUI插件css
import './mui-master/examples/hello-mui/css/icons-extra.css' //MUI图标扩展插件css
import './mui-master/dist/css/iconfont.css' //添加外置图标
// import './mui-master/plugin/picker/css/mui.picker.css'
// import poppicker from './mui-master/plugin/picker/js/mui.poppicker.js'
import App from './App'
import myHead from './components/myhead'
import myBottom from './components/mybottom'
import router from './router.js'

Vue.use(Vuex)
NutUI.install(Vue)
Vue.use(VueRouter)  //挂载安装路由到Vue实例中
Vue.use(VueResource)
Vue.use(MintUI)
Vue.use(NutUI)
Vue.config.productionTip = false
//全局注册组件
Vue.component("myhead",myHead);
Vue.component("mybottom",myBottom)

//vuex实例对象
const store = new Vuex.Store({
  state: {
    shopList: JSON.parse(localStorage.getItem("car")),
    carOf: JSON.parse(localStorage.getItem("carOf")),
    seet: JSON.parse(localStorage.getItem("seet")),
    orde: []
  },
  mutations: {
    addCar(state, shoCar){ //处理点击加入购物车的数据
      if(state.shopList == null){
        state.shopList = []
      }
      let listBool = false
      state.shopList.some(item => { //遍历数组，如果找到相匹配的就退出循环就退出
        if(item.Carname == shoCar.Carname){
          item.Carnum = parseInt(item.Carnum) + parseInt(shoCar.Carnum)
          localStorage.setItem("car",JSON.stringify(state.shopList))
          listBool = true
          return true
        }
      })
      if(!listBool){ //当没有在数组找到相同的数据
        state.shopList.push(shoCar)
        localStorage.setItem("car",JSON.stringify(state.shopList))
      }
    },
    getcarOf(state){  //处理购物车图标的数量
      state.carOf = 0
      state.shopList.forEach(element => {
        if(element.carBool){
          state.carOf += element.Carnum
        }
      });
      localStorage.setItem("carOf",JSON.stringify(state.carOf))
    },
    setBool(state,name){  //处理购物车选择按钮
      state.shopList.some(item => {
        if(item.Carname == name){
          item.carBool = !item.carBool
          if(!item.carBool){
            state.carOf -= item.Carnum
            let jg = (parseFloat(item.Carjg) * parseInt(item.Carnum)).toFixed(2)
            state.seet = (parseFloat(state.seet) - parseFloat(jg)).toFixed(2)
            localStorage.setItem("seet",JSON.stringify(state.seet))
          }else{
            state.carOf += item.Carnum
            let jg = (parseFloat(item.Carjg) * parseInt(item.Carnum)).toFixed(2)
            state.seet = (parseFloat(state.seet) + parseFloat(jg)).toFixed(2)
            localStorage.setItem("seet",JSON.stringify(state.seet))
          }
          localStorage.setItem("carOf",JSON.stringify(state.carOf))
          localStorage.setItem("car",JSON.stringify(state.shopList))
        }
      })
    },
    addcar(state, name){ //处理购物车加按钮
      state.shopList.some(item => {
        if(item.Carname == name){
          item.Carnum += 1
          localStorage.setItem("car",JSON.stringify(state.shopList))
          if(item.carBool){
            state.carOf += 1
            state.seet = (parseFloat(state.seet) + parseFloat(item.Carjg)).toFixed(2)
            localStorage.setItem("seet",JSON.stringify(state.seet))
            localStorage.setItem("carOf",JSON.stringify(state.carOf))
          }
        }
      })
    },
    redcar(state, name){  //处理购物车减按钮
      state.shopList.some(item => {
        if(item.Carnum > 1){
          if(item.Carname == name){
            item.Carnum -= 1
            localStorage.setItem("car",JSON.stringify(state.shopList))
            if(item.carBool){
              state.carOf -= 1
              state.seet = (parseFloat(state.seet) - parseFloat(item.Carjg)).toFixed(2)
              localStorage.setItem("seet",JSON.stringify(state.seet))
              localStorage.setItem("carOf",JSON.stringify(state.carOf))
            }
          }
        }
      })
    },
    delcar(state, name){  //处理购物车删除按钮
      state.shopList.some((item, key)=> {
        if(item.Carname == name){
          state.shopList.splice(key,1)
          if(item.carBool){
            state.carOf -= item.Carnum
            let jg = (parseFloat(item.Carjg) * parseInt(item.Carnum)).toFixed(2)
            state.seet = (parseFloat(state.seet) - parseFloat(jg)).toFixed(2)
            localStorage.setItem("seet",JSON.stringify(state.seet))
            localStorage.setItem("carOf",JSON.stringify(state.carOf))
          }
          localStorage.setItem("car",JSON.stringify(state.shopList))
        }
      })
    },
    seetcar(state){  //处理购物车结算的价格
      state.seet = 0
      if(state.shopList != null){
        state.shopList.forEach(item => {
          if(item.carBool){
            let shopnu = (parseFloat(item.Carjg)  * parseInt(item.Carnum)).toFixed(2)
            state.seet = (parseFloat(state.seet) + parseFloat(shopnu)).toFixed(2)
          }
        })
        localStorage.setItem("seet",JSON.stringify(state.seet))
      }
    },
    infoorde(state, data){ //处理商品信息的确认订单
      state.orde = []
      state.orde.push(data)
    },
    infoshoporde(state, data){ //处理购物车的确认订单
      state.orde = []
      if(data){
        data.forEach(item => {
          if(item.carBool){
            let shopdata = {ordename: item.Carname, ordejg: item.Carjg, ordeimg: item.CarImg, ordenum: item.Carnum}
            state.orde.push(shopdata)
          }
        })
      }
    }
  }
})

/* eslint-disable no-new */
new Vue({ //一个Vue实例对象
  el: '#app', //要控制的区域
  components: { App }, //要想可以调用模板，需要注册模板
  template: '<App/>',  //模板，对应的标签
  router,  //挂载路由对象
  store //挂载vuex对象
})
